package com.xmh.spendingPlan.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xmh.spendingPlan.dao.SpendingPlanDao;
import com.xmh.spendingPlan.service.SpendingPlanService;
import com.xmh.util.DateUtil;

@Service
public class SpendingPlanServiceImpl implements SpendingPlanService {

	@Autowired
	private SpendingPlanDao spendingPlanDao;

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> selectList() {
		List<Object> list = spendingPlanDao.selectCountList();

		if (list.isEmpty()) {
			return null;
		}
		Map<String, Object> firstObj = (Map<String, Object>) list.get(0);

		Map<String, Object> lastObj = (Map<String, Object>) list.get(list.size() - 1);

		Date minDate = (Date) firstObj.get("costDate");
		Date maxDate = (Date) lastObj.get("costDate");

		List<Object> fzList = new ArrayList<Object>();
		List<Object> hbList = new ArrayList<Object>();
		List<Object> jdbtList = new ArrayList<Object>();
		List<Object> yebList = new ArrayList<Object>();
		List<Object> fqlList = new ArrayList<Object>();
		List<Object> xykList = new ArrayList<Object>();

		List<Date> dates = DateUtil.getDates(minDate, maxDate);
		List<String> dateStringList = new ArrayList<String>();

		SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyy-MM");

		for (Date date : dates) {
			dateStringList.add(sdfFormat.format(date));

			fzList.add(null);
			hbList.add(null);
			jdbtList.add(null);
			yebList.add(null);
			fqlList.add(null);
			xykList.add(null);

			Iterator<Object> iterator = list.iterator();
			while (iterator.hasNext()) {
				Map<String, Object> map = (Map<String, Object>) iterator.next();

				Date costDate = (Date) map.get("costDate");
				Integer type_id = (Integer) map.get("typeId");
				BigDecimal amount = (BigDecimal) map.get("amount");

				if (DateUtil.isToMonth(costDate, date)) {
					if (type_id == 1) {
						hbList.set(hbList.size() - 1, amount);
					} else if (type_id == 2) {
						jdbtList.set(jdbtList.size() - 1, amount);
					} else if (type_id == 3) {
						fzList.set(fzList.size() - 1, amount);
					} else if (type_id == 4) {
						yebList.set(yebList.size() - 1, amount);
					} else if (type_id == 5) {
						fqlList.set(fqlList.size() - 1, amount);
					} else if (type_id == 6) {
						xykList.set(xykList.size() - 1, amount);
					}
					iterator.remove();
				}
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("fzList", fzList);
		map.put("hbList", hbList);
		map.put("jdbtList", jdbtList);
		map.put("yebList", yebList);
		map.put("fqlList", fqlList);
		map.put("xykList", xykList);
		map.put("dates", dateStringList);

		return map;
	}

}
